# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.admin.food.canteen import Canteen
import time

@route(r'/admin/food/canteen/?')
class IndexHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        page = (int(cur_page) - 1) * self.offset

        ret, count = yield Canteen().select_canteen(school_id, page, self.offset)

        page_info = {'total': count, 'page': int(cur_page)}
        self.render("admin/food/canteen/list.html", page_info=page_info, data=ret)


@route(r'/admin/food/canteen/new/?')
class NewHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        self.render("admin/food/canteen/new.html")

    @asynchronous
    @gen.coroutine
    def post(self):
        ltype = self.get_argument('ltype', default='0')
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')

        canteen_info_data = {
            "Fname":         Fname,
            "Fschool_id":    school_id,
            "Fcreate_time":  time.time()
        }

        count = yield Canteen().insert_canteen(canteen_info_data)

        if ltype == '0':
            self.redirect('/admin/food/canteen/')
        else:
            self.redirect('/admin/food/canteen/new/')

@route(r'/admin/food/canteen/del/(?P<FcanteenId>[0-9].*)/(?P<cur_page>[0-9].*)', name='canteen.del_canteen')
class DelHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, FcanteenId, cur_page):
        school_id = self.get_argument('schoolId', default=self.school_id)

        ret = Canteen().del_canteen(FcanteenId)

        self.redirect('/admin/food/canteen/?page=' + cur_page)

@route(r'/admin/food/canteen/edit/(?P<Fid>[0-9].*)', name='canteen.edit')
class EditHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)

        canteen = yield Canteen().find_canteen(Fid)

        self.render("admin/food/canteen/edit.html", canteen=canteen)

    @asynchronous
    @gen.coroutine
    def post(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')

        canteen_info_data = {
            "Fname":         Fname,
            "Fschool_id":    school_id,
            "Fcreate_time":  time.time()
        }

        count = yield Canteen().update_canteen(Fid, canteen_info_data)

        self.redirect('/admin/food/canteen/')